//
//  YKCompTabController.m
//  HMPersonalDoctor
//
//  Created by 叶文凯 on 16/9/10.
//  Copyright © 2016年 geduo. All rights reserved.
//

#import "YKCompTabController.h"
#import "CCKYTools.h"
#import "YKComplicationModel.h"


@interface YKCompTabController ()


@end

@implementation YKCompTabController
{
    NSArray<YKComplicationModel *> *_compList;
    
    NSMutableArray *_selectNameList;
    NSMutableArray *_buttonlist;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];

    _selectNameList = [NSMutableArray array];
    _buttonlist = [NSMutableArray array];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction:)];
    
    self.navigationItem.rightBarButtonItem = item;
    
    
    self.navigationItem.title = @"并发症状";
    
    
    
}

- (void)saveAction:(UIBarButtonItem *)sender
{
    
    NSArray *namelist = _selectNameList.copy;
    if (self.compBlock) {
        
        self.compBlock(namelist);
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    

    
}

- (void)loadData
{
    CCKYTools *tool = [CCKYTools sharedTools];
    
    [tool complicationListWithSuccess:^(NSArray *response) {
        
        //        NSLog(@"%@",response);
        
        _compList =  [NSArray yy_modelArrayWithClass:[YKComplicationModel class] json:response];

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
            [self.tableView reloadData];
            
        }];

    }];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _compList.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.textLabel.text = _compList[indexPath.row].complication_name;
    
    UIButton *selectButton = [[UIButton alloc] init];
    [selectButton setTitle:@"确认" forState:UIControlStateSelected];
    [selectButton setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    
    [selectButton setTitle:@"确认" forState:UIControlStateNormal];
    [selectButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [selectButton sizeToFit];

    [_buttonlist addObject:selectButton];
    
    cell.accessoryView = selectButton ;

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     YKComplicationModel *model = _compList[indexPath.row];
    
    if ([_selectNameList containsObject:model.complication_name] ) {
        return;
    }
 
    [_selectNameList addObject:model.complication_name];
    
   
    UIButton *button = _buttonlist[indexPath.row];
    button.selected = YES;
    
    
}




@end
